1. Factorial of a Given Number
# Method 1: Using for loop
num = int(input("Enter a number: "))
factorial = 1
if num < 0:
print("Factorial doesn't exist for negative numbers")
elif num == 0:
print("Factorial of 0 is 1")
else:
for i in range(1, num + 1):
factorial *= i
print(f"Factorial of {num} is {factorial}")
# Method 2: Using while loop
num = int(input("Enter a number: "))
factorial = 1
i = 1
while i <= num:
factorial *= i
i += 1
print(f"Factorial of {num} is {factorial}")
Sample Output:
Enter a number: 5 Factorial of 5 is 120
2. Armstrong Number Checker
num = int(input("Enter a number: "))
temp = num
sum_of_cubes = 0
num_digits = len(str(num))
while temp > 0:
digit = temp % 10
sum_of_cubes += digit ** num_digits
temp //= 10
if sum_of_cubes == num:
print(f"{num} is an Armstrong number")
else:
print(f"{num} is not an Armstrong number")
Sample Output:
Enter a number: 153 153 is an Armstrong number Enter a number: 123 123 is not an Armstrong number
Note: Armstrong number is a number equal to the sum of its digits raised to the power of number of digits (e.g., 153 = 1³ + 5³ + 3³)
3. Fibonacci Series
n = int(input("Enter number of terms: "))
a, b = 0, 1
count = 0
if n <= 0:
print("Please enter a positive integer")
elif n == 1:
print(f"Fibonacci series up to {n} term: {a}")
else:
print("Fibonacci series:")
while count < n:
print(a, end=" ")
c = a + b
a = b
b = c
count += 1
Sample Output:
Enter number of terms: 10 Fibonacci series: 0 1 1 2 3 5 8 13 21 34
4. Prime Number Checker
num = int(input("Enter a number: "))
if num <= 1:
print(f"{num} is not a prime number")
elif num == 2:
print(f"{num} is a prime number")
else:
is_prime = True
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
is_prime = False
break
if is_prime:
print(f"{num} is a prime number")
else:
print(f"{num} is not a prime number")
Sample Output:
Enter a number: 17 17 is a prime number Enter a number: 20 20 is not a prime number
5. Palindrome Number Checker
# Method 1: Using string reversal
num = int(input("Enter a number: "))
if str(num) == str(num)[::-1]:
print(f"{num} is a palindrome")
else:
print(f"{num} is not a palindrome")
# Method 2: Using mathematical approach
num = int(input("Enter a number: "))
temp = num
reverse = 0
while temp > 0:
digit = temp % 10
reverse = reverse * 10 + digit
temp //= 10
if num == reverse:
print(f"{num} is a palindrome")
else:
print(f"{num} is not a palindrome")
Sample Output:
Enter a number: 121 121 is a palindrome Enter a number: 123 123 is not a palindrome
6. Sum of Digits
num = int(input("Enter a number: "))
temp = num
sum_digits = 0
while temp > 0:
digit = temp % 10
sum_digits += digit
temp //= 10
print(f"Sum of digits of {num} is {sum_digits}")
Sample Output:
Enter a number: 12345 Sum of digits of 12345 is 15
7. Numbers Divisible by 5 or 7 (1-100)
count = 0
print("Numbers divisible by 5 or 7 between 1 and 100:")
for i in range(1, 101):
if i % 5 == 0 or i % 7 == 0:
print(i, end=" ")
count += 1
print(f"\n\nTotal count: {count}")
Sample Output:
Numbers divisible by 5 or 7 between 1 and 100: 5 7 10 14 15 20 21 25 28 30 35 40 42 45 49 50 55 56 60 63 65 70 75 77 80 84 85 90 95 98 100 Total count: 31
8. Convert Lowercase to Uppercase
# Method 1: Using built-in function
text = input("Enter a string: ")
print("Uppercase:", text.upper())
# Method 2: Manual conversion using loop
text = input("Enter a string: ")
result = ""
for char in text:
if 'a' <= char <= 'z':
result += chr(ord(char) - 32)
else:
result += char
print("Uppercase:", result)
Sample Output:
Enter a string: Hello World Uppercase: HELLO WORLD
9. Multiplication Table
num = int(input("Enter a number: "))
print(f"\nMultiplication table of {num}:")
print("-" * 30)
for i in range(1, 11):
print(f"{num} × {i} = {num * i}")
# For custom range
num = int(input("\nEnter a number: "))
start = int(input("Enter start range: "))
end = int(input("Enter end range: "))
print(f"\nMultiplication table of {num} from {start} to {end}:")
for i in range(start, end + 1):
print(f"{num} × {i} = {num * i}")
Sample Output:
Enter a number: 5 Multiplication table of 5: ------------------------------ 5 × 1 = 5 5 × 2 = 10 5 × 3 = 15 5 × 4 = 20 5 × 5 = 25 5 × 6 = 30 5 × 7 = 35 5 × 8 = 40 5 × 9 = 45 5 × 10 = 50
10. Number Pattern with Stars
n = 5 # Number of rows
for i in range(n, 0, -1):
# Print left ascending numbers
for j in range(1, i + 1):
print(j, end="")
# Print stars in middle
for k in range(n - i):
print("*", end="")
# Print right descending numbers
for j in range(i, 0, -1):
print(j, end="")
print() # New line after each row
Sample Output:
123454321 1234*4321 123**321 12***21 1****1
11. Sum of Series: 1 + 1/2 + 1/3 + ... + 1/n
n = int(input("Enter the number of terms: "))
sum_series = 0
print(f"\nSeries: ", end="")
for i in range(1, n + 1):
sum_series += 1 / i
if i == 1:
print(f"1", end="")
else:
print(f" + 1/{i}", end="")
print(f"\n\nSum of the series = {sum_series:.4f}")
# Alternative: Show each term value
n = int(input("\nEnter the number of terms: "))
sum_series = 0
print(f"\nTerms:")
for i in range(1, n + 1):
term = 1 / i
sum_series += term
print(f"Term {i}: 1/{i} = {term:.4f}")
print(f"\nTotal Sum = {sum_series:.4f}")
Sample Output:
Enter the number of terms: 5 Series: 1 + 1/2 + 1/3 + 1/4 + 1/5 Sum of the series = 2.2833 Enter the number of terms: 5 Terms: Term 1: 1/1 = 1.0000 Term 2: 1/2 = 0.5000 Term 3: 1/3 = 0.3333 Term 4: 1/4 = 0.2500 Term 5: 1/5 = 0.2000 Total Sum = 2.2833